package ace.module.account.core.impl.controller.rest.admin.ext;

import ace.cmp.core.model.R;
import ace.cmp.security.oauth2.resource.server.core.annotation.PreAuthorizeIsUserAuthenticated;
import ace.module.account.api.model.dto.IamAccountDetailDto;
import ace.module.account.api.model.input.RegisterQuickByUsernameInput;
import ace.module.account.core.impl.constant.AccountCoreConstant;
import ace.module.account.core.impl.service.IamAccountService;
import ace.module.security.api.AuthenticationApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * template author: caspar template created at: 2023-12-18 template description: admin controller
 * generated at: 2024年1月8日 下午3:53:01 description: 账户 admin controller
 */
@Tag(name = "账户-管理-API")
@PreAuthorizeIsUserAuthenticated
@AllArgsConstructor
@RestController
@RequestMapping(IamAccountExtAdminController.PATH)
@Valid
public class IamAccountExtAdminController {

  public static final String PATH = "/account/iam-account";
  public static final String PATH_PAGE_WITH_PROFILE = "/pageWithProfile";
  public static final String PATH_ADD_BY_USERNAME = "/addByUsername";
  public static final String URL_PAGE_WITH_PROFILE = PATH + PATH_PAGE_WITH_PROFILE;
  public static final String URL_ADD_BY_USERNAME = PATH + PATH_ADD_BY_USERNAME;
  public static final String ACCOUNT_ADMIN_READ =
      AccountCoreConstant.MODULE_NAME + "_account_read";
  public static final String ACCOUNT_ADMIN_WRITE =
      AccountCoreConstant.MODULE_NAME + "_account_write";
  private final IamAccountService iamAccountService;
  private final AuthenticationApi authenticationApi;

  @Operation(summary = "获取账户与资料", description = "查询别名：account与profile,权限：" + ACCOUNT_ADMIN_READ)
  @PreAuthorize("@securityService.hasAuthority('" + ACCOUNT_ADMIN_READ + "')")
  @GetMapping(PATH_PAGE_WITH_PROFILE)
  public R<Page<IamAccountDetailDto>> pageWithProfile(
      @RequestParam(value = "query", required = false) String query,
      @PageableDefault Pageable pageable) {
    Long iamAppId = null;
    Page<IamAccountDetailDto> responsePage = iamAccountService.pageWithProfile(query, iamAppId, pageable);
    return R.ok(responsePage);
  }

  @Operation(summary = "添加账户,根据账户与密码,返回账户id", description = "权限：" + ACCOUNT_ADMIN_WRITE)
  @PreAuthorize("@securityService.hasAuthority('" + ACCOUNT_ADMIN_WRITE + "')")
  @PostMapping(PATH_ADD_BY_USERNAME)
  public R<Long> addByUsername(@Valid @NotNull @RequestBody RegisterQuickByUsernameInput input) {
    Long result = iamAccountService.registerQuickByUsername(input);
    return R.ok(result);
  }
}
